<?php
/**
 * Created by PhpStorm.
 * Date: 2021/6/22
 * Time: 15:57
 */
namespace App\Http\Middleware;

use App\Http\Model\Hint;
use Closure;

/**
 * 返回结果执行加密
 */
class AesEncry
{
/**
 * 处理响应并返回AES加密结果
 * 
 * @param mixed $request 请求对象
 * @param Closure $next 管道闭包
 * @return string Base64编码的AES加密结果
 */
public function handle($request, Closure $next)
{
    try {
        $original = $next($request)->getOriginalContent();
        
        // 标准化响应数据
        if (is_array($original)) {
            [$code, $data, $msg] = array_values($original) + [0, [], ''];
        } elseif (is_scalar($original)) {
            [$code, $data, $msg] = [$original, [], ''];
        } else {
            [$code, $data, $msg] = [Hint::ERR_NORMAL, [], '不支持的响应类型'];
        }

        return preEnAES($code, $data, $msg);

    } catch (\Throwable $e) {
        return preEnAES(Hint::ERROR, ['error' => env('APP_DEBUG') ? $e->getMessage() : null],'请求处理失败');
    }
}
    
}
